/*************************************************************************
	> File Name: test2.cc
	> Author: H.M. Lau
	> Mail: hm.lau@outlook.com
	> Created Time: 2023/09/22 13:33
 ************************************************************************/

#include <iostream>
#include <unistd.h>
#include <cerrno>
#include <string>
#include <cstring>
#include <cstdio>
#include <pthread.h>

__thread int g_val = 0;

void *threadRoutine(void *args)
{
    int cnt = 10;
    while (cnt--) {
        std::cout << (char*)args << " : " << g_val << " &: " << &g_val << std::endl;
        ++g_val;
        sleep(1);
    }
    return nullptr;
}

int main(int argc, char *argv[])
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadRoutine, (void*)"thread 1");
    int cnt = 15;
    while (cnt--) {
        std::cout << "Main thread:" << g_val << " &: " << &g_val << std::endl;
        sleep(1);
    }
    int n = pthread_join(tid, nullptr);
    std::cout << "n:" << n << " errstring:" << strerror(n) << std::endl;
    return 0;
}
